package com.dhnsoft.数据结构与算法.栈与队列.队列;

import java.util.Scanner;

/**
 * ClassName: ArrayQueueDemo
 * Package: com.dhnsoft.数据结构与算法.栈与队列.队列
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/21 10:44
 * @Version 1.0
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出得数据是%d\n",res);
                        break;
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头数据是%d\n",res);
                        break;
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
            System.out.println("程序退出");
        }
    }
}

class ArrayQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    //创建队列的构造器
    public ArrayQueue(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.front = -1;
        this.rear = -1;
        arr = new int[arrMaxSize];
    }

    //判断队列是否为空
    public boolean isEmpty(){
        if(rear==front){
            return true;
        }
        return false;
    }

    //判断队列是否满了
    public boolean isFull(){
        return rear==maxSize-1;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列是否满了
        if(isFull()){
            throw new RuntimeException("队列满，不能加入数据");
        }
        arr[++rear] = n;
    }

    //获取队列数据
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，不能获取数据");
        }
        return arr[++front];
    }

    //遍历队列
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i=0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }

    //获取队列头节点
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空的，没有数据");
        }
        return arr[front+1];
    }

}

